<template>
  <div class="preview">
    <el-dialog title="预览" :visible.sync="dia_show" width="800px" :modal="true" :before-close="handleClose" :lock-scroll="true">
      <div class="showtitle">
        <p>站点名称：{{ info.title }}</p>
      </div>
      <div class="showbody">
        <div class="showleft">
          <iframe :src="url_header" frameborder="0" style="width: 100%;height: 100%" ref="iframe"></iframe>
        </div>
        <div class="showright">
          <p>预览</p>
          <p>
            扫描查看移动版
            <span style="color:#457eff" @click="copy">复制链接</span>
          </p>
          <div>
            <canvas id="QRCode"></canvas>
            <!-- <img src="./../../../../../assets/img/kf1.jpg" alt="" /> -->
          </div>
          <p>
            如页面加载有问题，请
            <span @click="refresh">
              <i class="el-icon-refresh" style="color:#457eff"></i>
              重新加载</span>
          </p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import QRCode from "qrcode";
export default {
  name: "previewbox",
  data () {
    return {
      url_header: "",
      dia_show: false
    };
  },
  props: {
    info: {
      type: Object,
      default: () => ({})
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  components: {},
  methods: {
    refresh () {
      //   this.$refs.iframe.contentWindow;
      var str = this.url_header;
      this.url_header = "";
      setTimeout(() => {
        this.url_header = str;
      }, 100);
      console.log(this.$refs.iframe.contentWindow);
    },
    copy () {
      var span = this.info.iframeurl;
      var oInput = document.createElement("input");
      oInput.value = span;
      document.body.appendChild(oInput);
      oInput.select();
      document.execCommand("Copy");
      oInput.className = "oInput";
      oInput.style.display = "none";
      this.$fnc.alertSuccess("复制成功");
    },
    handleClose () {
      this.dia_show = false;
      this.$emit("closedia");
    }
  },
  watch: {
    show (old, after) {
      this.dia_show = old;
      if (this.dia_show == true) {
        this.url_header = this.info.iframeurl;
        this.$nextTick(() => {
          var msg = document.getElementById("QRCode");
          // 将获取到的数据（val）画到msg（canvas）上
          QRCode.toCanvas(msg, this.info.iframeurl, function (error) {
            console.log(error);
          });
        });
      }
    }
  }
};
</script>
<style lang="less" scoped>
.showtitle {
  font-size: 16px;
  color: #457eff;
  padding-bottom: 10px;
  display: flex;
  justify-content: flex-start;
}
.showbody {
  width: 100%;

  height: 640px;
  display: flex;
  flex-wrap: nowrap;
  justify-content: flex-start;
  align-items: center;
  font-size: 16px;
  line-height: 26px;
  color: #333333;
  .showleft {
    width: 380px;
    height: 100%;
    margin-right: 35px;
  }
  .showright {
    width: 300px;
    height: 100%;
    display: flex;
    flex-flow: column;
    justify-content: flex-start;
    align-items: flex-start;
    > div {
      width: 250px;
      height: 250px;
      border: 1px solid #eeeeee;
      padding: 10px;
      margin: 15px 0;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 100%;
        height: 100%;
      }
      #QRCode {
        width: 240px !important;
        height: 240px !important;
        margin: 0 auto;
      }
    }
    > p > span {
      font-size: 16px;
      font-weight: bold;
      color: #457eff;
    }
  }
}
</style>
